# coding=utf-8
import os
import sys

def reSignIpa(p12File, p12Pwd, ipaName, appName, certificateName):
    # 安装证书
    cerName = certificateName
    if cerName == "":
        cerName = parseP12File(p12File, p12Pwd)
    print("证书名称:%s "%(cerName))
    #生成plist
    cmd1 = "security cms -D -i ./embedded.mobileprovision > embedded.plist"
    result1 = os.system(cmd1)
    if result1 == 0:
        print("1、embedded.plist 生成成功！！！🚀🚀🚀")

    cmd2 = "/usr/libexec/PlistBuddy -x -c 'Print:Entitlements' ./embedded.plist > ./entitlements.plist"
    result2 = os.system(cmd2)
    if result2 == 0:
        print("2、entitlements.plist 生成成功！！！🚀🚀🚀")

    # 解压ipa包
    cmd3 = "unzip ./%s.ipa"%(ipaName)
    result3 = os.system(cmd3)
    if result3 == 0:
        print("3、ipa包解压成功！！！")

    # 删除ipa中的签名文件
    cmd4 = "rm -rf ./Payload/%s.app/_CodeSignature"%(ipaName)
    result4 = os.system(cmd4)
    if result4 == 0:
        print("4、删除ipa中的签名文件成功")

    # 遍历动态库，删除签名文件 & 重签名 & 替换描述文件
    path = "./Payload/%s.app/Frameworks/"%(appName)
    files = os.listdir(path)
    for file in files:
        signFile = path + file + "/_CodeSignature"
        print(signFile)

        # 移除签名文件
        cmd5 = "rm -rf %s"%(signFile)
        result5 = os.system(cmd5)
        print("删除app签名文件成功！")

        cmd6 = 'codesign -f -s "%s" %s%s'%(cerName,path,file)
        result6 = os.system(cmd6)
        if result6 == 0:
            print("6、%s 重签名成功！！！！"%(file))
        else:
            print(result6)
            print("6、重签名失败！！！💣💣💣💣💣💣💣💣%s %s %s"%(cerName,path,file))

    # 替换app中的描述文件
    oldProvisionPath = "./Payload/%s.app/embedded.mobileprovision"%(appName)
    cmd7 = "cp ./embedded.mobileprovision %s"%(oldProvisionPath)
    result7 = os.system(cmd7)
    if result7 == 0:
        print("7.替换app描述文件成功！！！")

    # # app重签名
    cmd8 = 'codesign -f -s "%s" --no-strict --entitlements=entitlements.plist ./Payload/%s.app'%(cerName,appName)
    result8 = os.system(cmd8)
    if result8 == 0:
        print("8、重签名成功！！！ 🎉🎉🎉🎉🎈🎈🎈🎈")
    else:
        print("8、重签名失败！！！💣💣💣💣💣💣💣💣")

    # # 重新压缩打包app
    cmd9 = 'zip -r ./%s_new.ipa ./Payload'%(appName)
    result9 = os.system(cmd9)
    if result9 == 0:
        print("9、包压缩成功！！！ 🎉🎉🎉🎉🎈🎈🎈🎈")

def parseP12File(p12File,password):
    print("-2、安装签名证书进钥匙串：%s %s"%(p12File, password))
    importResult = os.system("security import ./%s -k ~/Library/Keychains/login.keychain -P \"%s\" -T /usr/bin/codesign"%(p12File,password))
    print("-1、导入p12结果：%s"%(importResult))
    if importResult != 0:
        print("导入p12失败")
    infoResult = os.popen("openssl pkcs12 -nodes -in ./%s -info -nokeys -passin 'pass:%s' 2>/dev/null | grep 'friendlyName'"%(p12File,password))
    friendlyName = infoResult.readline()
    friendlyName = friendlyName.replace("friendlyName:","").strip()
    print("0、签名证书名：%s"%(friendlyName))
    return friendlyName

if __name__ == "__main__":
    command = sys.argv[1]
    if command == "-s":
        reSignIpa(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
    else:
        # 包名
        ipaName = "Geometric_4.2.21_4"
        appName = "Geometric"
        # p12 Url & pwd
        p12Url = "geometric_store_cer.p12"
        p12Pwd = "geely1234"

        reSignIpa(p12Url,p12Pwd, ipaName, appName, '')

